<?php
/**
 * Flexihash 类的作用只是 把大量数据 经过哈希算法 均匀的分配存储到 不同的redis数据库中 并不是redis集群
 *
 * Created by PhpStorm.
 * User: lipeng
 * Date: 16/9/14
 * Time: 下午5:26
 */

require "./vendor/autoload.php";

$config['redis_hash'][0]['host'] = "127.0.0.1";
$config['redis_hash'][0]['port'] = 7001;

$config['redis_hash'][1]['host'] = "127.0.0.1";
$config['redis_hash'][1]['port'] = 7002;

$config['redis_hash'][2]['host'] = "127.0.0.1";
$config['redis_hash'][2]['port'] = 7003;

$config['redis_hash'][3]['host'] = "127.0.0.1";
$config['redis_hash'][3]['port'] = 7004;

$config['redis_hash'][4]['host'] = "127.0.0.1";
$config['redis_hash'][4]['port'] = 7005;

$config['redis_hash'][5]['host'] = "127.0.0.1";
$config['redis_hash'][5]['port'] = 7006;

$flexihash = new \Flexihash\Flexihash();
$targets = array();
foreach($config['redis_hash'] as $k=>$redis){
    $targets[]=$k;
}

$res1 = $flexihash->addTargets($targets);

$all_targets = $flexihash->getAllTargets();

for ($i=0;$i<=10000;$i++){
    $key = $flexihash->lookup('lookup-'.$i);  //lookup 相当于hash key 会自动算出一个 addTargets 中添加的数组下标key

    $res_conf = $config['redis_hash'][$key];
    $redis = new  Redis();

    $object = $redis->pconnect($res_conf['host'], $res_conf['port'], 20);
    if ($object == false) {
        exit('连接失败');
    }

    $res = $redis->set('username-'.$i,'李小龙'.$i);
}





